home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / magazine / executive_v2.00 / data / developers.lzx / SysInfo / examples / test / test.c < prev   
C/C++ Source or Header  |  2003-04-17  |  8KB  |  305 lines

  1. /*
  2.  * Test-program for some SysInfo.library features
  3.  *
  4.  * This file is public domain.
  5.  *
  6.  * Author: Petri Nordlund <petrin@megabaud.fi>
  7.  *
  8.  * $Id: test.c 1.5 1996/10/01 23:04:02 petrin Exp petrin $
  9.  *
  10.  */
  11.  
  12. #include "defs.h"
  13. #include <proto/SysInfo.h>
  14. #include <libraries/SysInfo.h>
  15.  
  16.  
  17. /** VARIABLES **/
  18. struct SysInfo *si = NULL;
  19. struct Library *SysInfoBase = NULL;
  20.  
  21.  
  22.  
  23. int
  24. main(int argc, char **argv)
  25. {
  26.     if(!(SysInfoBase = OpenLibrary(SYSINFONAME, SYSINFOVERSION)))
  27.     {
  28.         puts("Can't open SysInfo.library");
  29.         exit(RETURN_FAIL);
  30.     }
  31.  
  32.  
  33. /**
  34.  ** Initialize the SysInfo.library, this will make connection to the
  35.  ** server-process and allocate the SysInfo-structure.
  36.  **
  37.  **/
  38.  
  39.     if(!(si = InitSysInfo()))
  40.     {
  41.         puts("Can't initialize SysInfo");
  42.         CloseLibrary(SysInfoBase);
  43.         exit(RETURN_FAIL);
  44.     }
  45.  
  46.  
  47. /**
  48.  ** Print this task's pid, ppid and pgrp.
  49.  **
  50.  **/
  51.  
  52.     printf("pid:  %d\n",GetPid(si));
  53.  
  54.     if(si->GetPpid_implemented)
  55.     {
  56.         LONG p = GetPpid(si);
  57.         if(p != -1)
  58.             printf("ppid: %d\n",p);
  59.         else
  60.             printf("ppid: unknown\n");
  61.     }
  62.  
  63.     if(si->GetPgrp_implemented)
  64.     {
  65.         LONG p = GetPgrp(si);
  66.         if(p != -1)
  67.             printf("pgrp: %d\n",p);
  68.         else
  69.             printf("pgrp: unknown\n");
  70.     }
  71.  
  72.  
  73. /**
  74.  ** If si->which_implemented is 0, then GetNice() and SetNice() are not
  75.  ** available. We'll also make sure that the search methods we need have
  76.  ** been implemented.
  77.  **
  78.  **/
  79.  
  80.     if(si->which_implemented && (si->which_implemented & (WHICHF_PRIO_TASK | WHICH_PRIO_PROCESS)))
  81.     {
  82.         LONG nice;
  83.  
  84.         /** Display the nice-value for this task **/
  85.         nice=GetNice(si,WHICH_PRIO_TASK,0);
  86.         if(nice == -1)
  87.         {
  88.             if(si->errno)
  89.                 printf("GetNice() failed, errno: %d\n",si->errno);
  90.             else
  91.                 printf("nice: %d\n",nice);
  92.         }
  93.         else
  94.             printf("nice: %d\n",nice);
  95.  
  96.         /** Set our nice-value to +5 **/
  97.         if(SetNice(si,WHICH_PRIO_PROCESS,GetPid(si),5))
  98.             printf("SetNice() failed, errno: %d\n",si->errno);
  99.     }
  100.  
  101.  
  102. /**
  103.  ** Ask for notification and output load averages every second for
  104.  ** 10 seconds.
  105.  **
  106.  **/
  107.  
  108.     if(si->loadavg_type != LOADAVG_NONE)
  109.     {
  110.         struct SI_Notify *not;
  111.         struct Message *msg;
  112.         short i;
  113.  
  114.         if(si->notify_msg_implemented && (not=AddNotify(si,AN_USE_MESSAGES,10)))
  115.         {
  116.             printf("load averages (%d.%02d, %d.%02d, %d.%02d minutes):\n",
  117.                 si->loadavg_time1/60, si->loadavg_time1%60,
  118.                 si->loadavg_time2/60, si->loadavg_time2%60,
  119.                 si->loadavg_time3/60, si->loadavg_time3%60);
  120.  
  121.             for(i=0;i<10;i++)
  122.             {
  123.                 /**
  124.                  ** We'll get a message every second. There may be more than
  125.                  ** one message in the port at once.
  126.                  **
  127.                  **/
  128.  
  129.                 Wait(1L<<not->notify_port->mp_SigBit);
  130.  
  131.                 while(msg = GetMsg(not->notify_port))
  132.                 {
  133.                     /** This will be filled by GetLoadAverage() **/
  134.                     struct SI_LoadAverage load;
  135.  
  136.                     ReplyMsg(msg);
  137.  
  138.                     /** Ask SysInfo.library for current load averages **/
  139.                     GetLoadAverage(si, &load);
  140.  
  141.                     printf("load average:");
  142.  
  143.                     if(si->loadavg_type == LOADAVG_FIXEDPNT)
  144.                     {
  145.                         /** Convert fixed point values to floating point values **/
  146.  
  147.                         if(si->loadavg_time1)
  148.                             printf(" %.2f",(float) load.lavg_fixed.load1 / (float) si->fscale);
  149.                         else
  150.                             printf(" N/A");
  151.  
  152.                         if(si->loadavg_time2)
  153.                             printf(" %.2f",(float) load.lavg_fixed.load2 / (float) si->fscale);
  154.                         else
  155.                             printf(" N/A");
  156.  
  157.                         if(si->loadavg_time3)
  158.                             printf(" %.2f\n",(float) load.lavg_fixed.load3 / (float) si->fscale);
  159.                         else
  160.                             printf(" N/A\n");
  161.                     }
  162.                 }
  163.             }
  164.             RemoveNotify(si,not);
  165.         }
  166.         else
  167.             printf("Can't use notification.\n");
  168.     }
  169.     else
  170.         printf("Load averages are not supported.\n");
  171.  
  172.  
  173. /**
  174.  ** Output CPU usage values.
  175.  **
  176.  **/
  177.  
  178.     {
  179.         struct SI_CpuUsage cu;
  180.  
  181.         GetCpuUsage(si,&cu);
  182.  
  183.         printf("cpu time:                 ");
  184.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTAL_IMPLEMENTED)
  185.             printf("%d seconds used, %d seconds idle\n",cu.total_used_cputime, cu.total_elapsed_time - cu.total_used_cputime);
  186.         else
  187.             printf("N/A\n");
  188.  
  189.         printf("cpu usage:                ");
  190.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTAL_IMPLEMENTED)
  191.             printf("%.2f%%\n",((float) cu.total_used_cputime * 100.0) / (float) cu.total_elapsed_time);
  192.         else
  193.             printf("N/A\n");
  194.  
  195.         printf("current cpu usage:        ");
  196.         if(si->cpu_usage_implemented & CPU_USAGEF_LASTSEC_IMPLEMENTED)
  197.             printf("%.2f%%\n",((float) cu.used_cputime_lastsec * 100.0) / (float) cu.used_cputime_lastsec_hz);
  198.         else
  199.             printf("N/A\n");
  200.  
  201.         printf("recent cpu usage:         ");
  202.         if(si->cpu_usage_implemented & CPU_USAGEF_RECENT_IMPLEMENTED)
  203.             printf("%.2f%% (%d seconds)\n",((float) cu.recent_used_cputime * 100.0) / (float) cu.recent_used_cputime_hz, cu.recent_seconds);
  204.         else
  205.             printf("N/A\n");
  206.  
  207.         printf("context switches:         ");
  208.         if(si->cpu_usage_implemented & CPU_USAGEF_IVVOCSW_IMPLEMENTED)
  209.             printf("%d involuntary, %d voluntary\n", cu.involuntary_csw, cu.voluntary_csw);
  210.         else
  211.             printf("N/A\n");
  212.  
  213.         printf("total context switches:   ");
  214.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTALCSW_IMPLEMENTED)
  215.             printf("%d\n", cu.total_csw);
  216.         else
  217.             printf("N/A\n");
  218.  
  219.         printf("current context switches: ");
  220.         if(si->cpu_usage_implemented & CPU_USAGEF_IVVOCSW_LASTSEC_IMPLEMENTED)
  221.             printf("%d involuntary, %d voluntary\n", cu.involuntary_csw_lastsec, cu.voluntary_csw_lastsec);
  222.         else
  223.             printf("N/A\n");
  224.  
  225.         printf("current total csws:       ");
  226.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTALCSW_LASTSEC_IMPLEMENTED)
  227.             printf("%d\n", cu.total_csw_lastsec);
  228.         else
  229.             printf("N/A\n");
  230.     }
  231.  
  232.  
  233. /**
  234.  ** Output CPU usage values for this task.
  235.  **
  236.  **/
  237.  
  238.     {
  239.         struct SI_TaskCpuUsage cu;
  240.  
  241.         if(!GetTaskCpuUsage(si,&cu,0))
  242.         {
  243.             printf("This task:\n");
  244.  
  245.             printf("cpu time:                   ");
  246.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTAL_IMPLEMENTED)
  247.                 printf("%d.%d seconds\n",cu.total_used_cputime / cu.total_used_time_hz, cu.total_used_cputime % cu.total_used_time_hz);
  248.             else
  249.                 printf("N/A\n");
  250.  
  251.             printf("cpu usage:                  ");
  252.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTAL_IMPLEMENTED)
  253.                 printf("%.2f%%\n",(((float) cu.total_used_cputime) / ((float) cu.total_used_time_hz) * 100.0) / (float) cu.total_elapsed_time);
  254.             else
  255.                 printf("N/A\n");
  256.  
  257.             printf("current cpu usage:          ");
  258.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_LASTSEC_IMPLEMENTED)
  259.                 printf("%.2f%%\n",((float) cu.used_cputime_lastsec * 100.0) / (float) cu.used_cputime_lastsec_hz);
  260.             else
  261.                 printf("N/A\n");
  262.  
  263.             printf("recent cpu usage:           ");
  264.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_RECENT_IMPLEMENTED)
  265.                 printf("%.2f%% (%d seconds)\n",((float) cu.recent_used_cputime * 100.0) / (float) cu.recent_used_cputime_hz, cu.recent_seconds);
  266.             else
  267.                 printf("N/A\n");
  268.  
  269.             printf("context switches:           ");
  270.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_IVVOCSW_IMPLEMENTED)
  271.                 printf("%d involuntary, %d voluntary\n", cu.involuntary_csw, cu.voluntary_csw);
  272.             else
  273.                 printf("N/A\n");
  274.  
  275.             printf("total context switches:     ");
  276.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTALCSW_IMPLEMENTED)
  277.                 printf("%d\n", cu.total_csw);
  278.             else
  279.                 printf("N/A\n");
  280.  
  281.             printf("context switches (ps):      ");
  282.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_IVVOCSW_LASTSEC_IMPLEMENTED)
  283.                 printf("%d involuntary, %d voluntary\n", cu.involuntary_csw_lastsec, cu.voluntary_csw_lastsec);
  284.             else
  285.                 printf("N/A\n");
  286.  
  287.             printf("total context switches (ps):");
  288.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTALCSW_LASTSEC_IMPLEMENTED)
  289.                 printf("%d\n", cu.total_csw_lastsec);
  290.             else
  291.                 printf("N/A\n");
  292.         }
  293.         else
  294.             printf("Can't get CPU usage for this task.\n");
  295.     }
  296.  
  297.     if(si)
  298.         FreeSysInfo(si);
  299.  
  300.     if(SysInfoBase)
  301.         CloseLibrary(SysInfoBase);
  302.  
  303.     return(RETURN_OK);
  304. }
  305.